<?php
class autoload{
//	private  $seachedFile;
	public function __autoload($class){
		$file=$this->findFile($class);
		if($file){
			require_once($file);
		}
	}
	public function __set($vara,$value){
		$this->$vara=$value;
	}
	public function __get($vara){
		if(!isset($this->$vara)){
			$log = new Log();
			$log->write("The varable $vara is not existed in class ".get_class($this));
		}
		return $this->$vara;
	}
	public function findFile($class,$dir=''){
		$classFile = $class.".php";
		$targetFile='';
		if(file_exists($classFile)){
			return $classFile;
		}
		if(stripos($classFile,"_",0)>0){
			$targetFile = ($this->getBaseDir())."/".(str_replace("_","/",$classFile));
			if(file_exists($targetFile)){
				return $targetFile;
			}
		}
		if (stripos($class,'page',0)>0){
			$targetFile = str_replace('page','.class',$class).".php";
			$serachFileTemp=$this->readDirectory('controllers',$targetFile);
			if($serachFileTemp){
				return $serachFileTemp;
			}
		}
		if($dir){
			$targetFile=$this->readDirectory($dir,$classFile);
			if($targetFile&&!is_array($targetFile)){
				return $targetFile;
			}
		}
		$firstDirectories = array('lib','dao','controllers','includes','helper','Downloadnew','download','');
		foreach($firstDirectories as $singleDirectory){
			$targetFile=$this->readDirectory($singleDirectory,$classFile);
			if($targetFile){
				return $targetFile;
			}
		}
		return false;
	}
	public function readDirectory($dir='',$findFile='',$all=false){
		$baseDir=$this->getBaseDir($dir);
		if(is_dir($baseDir)){
			$finded = $this->getDirectoryFile($baseDir,$findFile,$all);
			if($finded!=''&&!is_array($finded)&&$findFile){			
				return $finded;
			}else if($finded!=''&&is_array($finded)&&!$findFile){
				return $finded;
			}
		}
	}
	public function getDirectoryFile($dir,$findFile='',$all=false){
		$path=$dir."/";
		  // var_dump("path:::".$path);
		 $targetFinded='';
		if(is_dir($dir)){
			$dirTemp = opendir($dir);
			while (($file = readdir($dirTemp)) !== false){
				$tempFile=$path.$file;
				if(!is_dir($tempFile)&&$file!="."&&$file!=".."&&$file!=".svn"&&!$findFile){
					if(isset($this->files)){
						$this->files[]=$tempFile;
					}else{
						$this->files=array($tempFile);
					}
				}
				if($findFile==$file){
					$targetFinded=$path.$findFile;
					if (!$all){
						break;
					}else{
						if(!isset($this->allSerched)){
							$this->allSerched=array();
						}
						$this->allSerched[]=$targetFinded;
					}
				}
				if(is_dir($tempFile)&&$file!="."&&$file!=".."&&$file!=".svn"){
					if(isset($this->files)){
						$this->files[]=$tempFile."/";
					}else{
						$this->files=array(($tempFile."/"));
					}
					$childDir = $path.$file;
					$targetFinded = $this->getDirectoryFile($childDir,$findFile,$all);
					if($targetFinded&&!$all){
						return $targetFinded;
					}
				}
			}
			closedir($dirTemp);
		}
		if($targetFinded){
			return $targetFinded;
		}else if(isset($this->files)&&!$findFile){
			return $this->files;
		}
	}
	public function searchFile($dir='',$findFile='',$all=true){
		 $this->readDirectory($dir,$findFile,$all);
		 return $this->allSerched;
	}
	public function getBaseDir($dir=''){
		if($dir==''){
			$currentScript = $_SERVER['PHP_SELF'];
			$currentFileArray = explode("/",$currentScript);
			if(count($currentFileArray)!=2){
				$baseDir =$_SERVER['DOCUMENT_ROOT']."/".$currentFileArray[1];
			}else{
				$baseDir=$_SERVER['DOCUMENT_ROOT'];
			}
			$baseDir=$this->prepareDir($baseDir);
		}else{
			$baseDir=$this->getBaseDir()."/".$dir;
			$baseDir=$this->prepareDir($baseDir);
		}
		return $baseDir;
	}
	public function getConfig(){
		if(file_exists(($this->getBaseDir())."/"."xml/system.xml")){
			return ($this->getBaseDir())."/"."xml/system.xml";
		}else {
			return $this->readDirectory('config','system.xml');
		}
	}
	public function prepareDir($dir){
		if (stripos($dir,"//",0)){
			$replecePattern ="|\/\/|";
			$dir=preg_replace($replecePattern,'/',$dir);
		}
		if (stripos($dir,"\\",0)){
			$replecePattern ="|\\\\|";
			$dir=preg_replace($replecePattern,'/',$dir);
		}
		return $dir;
	}
}
?>